package com.im.partchat.mvp.persenter;

import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;

import com.im.partchat.base.IBaseView;
import com.im.partchat.bean.FriendApply;
import com.im.partchat.bean.reponsed.BaseResponse;
import com.im.partchat.mvp.model.friend.FriendModel;
import com.im.partchat.mvp.model.friend.FriendModelImpl;
import com.im.partchat.utils.rxlifecycle.compact.RxLifecycleCompact;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;

/**
 * Created by hhz on 2017/7/3.
 */
public class FriendPresenter extends BasePresenter {

    private IBaseView baseView;
    private FriendModel friendModel;
    private AppCompatActivity context;

    public FriendPresenter(IBaseView baseView, AppCompatActivity activity) {
        this.baseView = baseView;
        this.friendModel = new FriendModelImpl();
        this.context = activity;
    }

    public void friendApplyAdd(String userId, String friendId) {
        friendModel.friendApplyAdd(userId, friendId)
                .compose(RxLifecycleCompact.bind(context).<BaseResponse<Object>>withObservable())
                .subscribe(new Observer<BaseResponse<Object>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        baseView.showLoading("正在提交好友申请");
                    }

                    @Override
                    public void onNext(BaseResponse<Object> value) {
                        if (value.getCode().equals("1")) {
                            baseView.onLoadDataSuccess("好友申请已提交", "friendApplyAdd");
                        } else {
                            String msg = value.getMsg();
                            if (TextUtils.isEmpty(msg)) {
                                msg = "好友申请提交失败";
                            }
                            baseView.showError(msg);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        baseView.hideLoading();
                        baseView.showError(NetErrorObservable(e));
                        e.printStackTrace();
                    }

                    @Override
                    public void onComplete() {
                        baseView.hideLoading();
                    }
                });

    }

    public void friendApplyConfirm(String applyId, boolean result) {
        friendModel.friendApplyConfirm(applyId, result)
                .compose(RxLifecycleCompact.bind(context).<BaseResponse<Object>>withObservable())
                .subscribe(new Observer<BaseResponse<Object>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        baseView.showLoading("正在提交好友申请");
                    }

                    @Override
                    public void onNext(BaseResponse<Object> value) {
                        if (value.getCode().equals("1")) {
                            baseView.onLoadDataSuccess("好友申请已提交", "friendApplyConfirm");
                        } else {
                            String msg = value.getMsg();
                            if (TextUtils.isEmpty(msg)) {
                                msg = "好友申请提交失败";
                            }
                            baseView.showError(msg);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        baseView.hideLoading();
                        baseView.showError(NetErrorObservable(e));
                        e.printStackTrace();
                    }

                    @Override
                    public void onComplete() {
                        baseView.hideLoading();
                    }
                });
    }

    public void friendDelete(String userId, String friendId) {
        friendModel.friendDelete(userId, friendId)
                .compose(RxLifecycleCompact.bind(context).<BaseResponse<Object>>withObservable())
                .subscribe(new Observer<BaseResponse<Object>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        baseView.showLoading("正在删除好友");
                    }

                    @Override
                    public void onNext(BaseResponse<Object> value) {
                        if (value.getCode().equals("1")) {
                            baseView.onLoadDataSuccess("删除好友成功", "friendApplyAdd");
                        } else {
                            String msg = value.getMsg();
                            if (TextUtils.isEmpty(msg)) {
                                msg = "删除失败";
                            }
                            baseView.showError(msg);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        baseView.hideLoading();
                        baseView.showError(NetErrorObservable(e));
                        e.printStackTrace();
                    }

                    @Override
                    public void onComplete() {
                        baseView.hideLoading();
                    }
                });
    }

    public void friendApplyList(String userId) {
        friendModel.friendApplyList(userId)
                .compose(RxLifecycleCompact.bind(context).<BaseResponse<List<FriendApply>>>withObservable())
                .map(new Function<BaseResponse<List<FriendApply>>, List<FriendApply>>() {
                    @Override
                    public List<FriendApply> apply(BaseResponse<List<FriendApply>> listBaseResponse) throws Exception {
                        if (listBaseResponse.getCode().equals("1")) {
                            return listBaseResponse.getData();
                        } else {
                            String msg = listBaseResponse.getMsg();
                            if (TextUtils.isEmpty(msg)) {
                                msg = "获取好友申请列表失败";
                            }
                            baseView.showError(msg);
                        }
                        return null;
                    }
                })
                .subscribe(new Observer<List<FriendApply>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        baseView.showLoading("正在获取好友申请列表");
                    }

                    @Override
                    public void onNext(List<FriendApply> value) {
                        baseView.onLoadDataSuccess(value, "friendApplyList");
                    }

                    @Override
                    public void onError(Throwable e) {
                        baseView.hideLoading();
                        baseView.showError(NetErrorObservable(e));
                        e.printStackTrace();
                    }

                    @Override
                    public void onComplete() {
                        baseView.hideLoading();
                    }
                });
    }
}
